package com.monco.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monco.common.response.ApiResult;
import com.monco.entity.Notice;
import com.monco.service.INoticeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author com/monco
* @since 2020-04-19
*/
@Api(tags = "公告模块")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private INoticeService noticeService;

    /**
     * 新增Notice方法
     *
     * @param notice
     * @return
     */
    @PostMapping
    public ApiResult save(@RequestBody Notice notice) {
        noticeService.save(notice);
        return ApiResult.ok();
    }

    /**
     * 修改Notice方法
     *
     * @param notice
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return ApiResult.ok();
    }

    /**
     * 查询列表
     *
     * @param notice
     * @return
     */
    @GetMapping("list")
    public ApiResult list(Notice notice) {
        return ApiResult.ok(noticeService.getNoticeList(notice));
    }

    /**
     * 查询分页
     *
     * @param current 当前页
     * @param size    一页多少数据
     * @param notice
     * @return
     */
    @GetMapping("page")
    public ApiResult page(@RequestParam(required = false, defaultValue = "0") long current,
                          @RequestParam(required = false, defaultValue = "10") long size, Notice notice) {
        return ApiResult.ok(noticeService.getNoticePage(new Page<Notice>().setCurrent(current).setSize(size), notice));
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ApiResult delete(@RequestParam Long id) {
        noticeService.removeById(id);
        return ApiResult.ok();
    }

    /**
     * 按照id获取单条记录
     *
     * @param id
     * @return
     */
    @GetMapping
    public ApiResult getOne(@RequestParam Long id) {
        return ApiResult.ok(noticeService.getById(id));
    }
}
